package manage.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import manage.dao.PayMapper;
import manage.model.Pay;
import manage.util.PayUtil;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.rmi.CORBA.Util;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.List;

@Service
public class PayServiceImpl {
    @Autowired
    private PayMapper payMapper;

    public PageInfo<Pay> selectAll(String page, String limit) {

        int pu = Integer.parseInt(page);
        int li = Integer.parseInt(limit);

        PageHelper.startPage(pu,li);
        List<Pay> list = payMapper.selectAll();
        list = this.util(list);
        PageInfo<Pay> pageInfo = new PageInfo<Pay>(list);
        return pageInfo;
    }
    public List<Pay> selectAll() {
        List<Pay> list = payMapper.selectAll();
        list = this.util(list);
        return list;
    }

    private List<Pay> util(List<Pay> list) {
        for (Pay pay:list) {
            double pay01 = Double.parseDouble(pay.getBasewage())-Double.parseDouble(pay.getBasewage())* PayUtil.readProperty2()/100;
            pay01 = (double) Math.round(pay01 * 100) / 100;
            pay.setPaywage(pay01+"");
        }
        return list;
    }

    public String insert(Pay record) {

        int i = payMapper.insert(record);
        if(i>0)
            return "success";
        return "fail";
    }

    public String del(Integer id) {
        int i = payMapper.deleteByPrimaryKey(id);
        if(i>0)
            return "success";
        return "fail";
    }

    public Pay select(Integer id) {
        return payMapper.selectByPrimaryKey(id);
    }
    public String update(Pay record){
        if(payMapper.updateByPrimaryKey(record)>0)
            return "success";
        return "fail";
    }

    public void exportExcel(List<Pay> payList, OutputStream outputStream) throws IOException {
        //1.创建工作簿
        HSSFWorkbook hwb =new HSSFWorkbook();
        //1.1创建合并单元格
        //CellRangeAddress cellRangeAddress =new CellRangeAddress(0,0,0,4);
        //2.创建工作表
        HSSFSheet sheet = hwb.createSheet("发放信息表");
        //2.1添加合并单元格
        //sheet.addMergedRegion(cellRangeAddress);

        //3.2创建第二行及单元格
        HSSFRow row2 = sheet.createRow(0);
        String[] row2Cell = {"id","员工编号","员工姓名","发放日期","实际工资","缴后工资","税后工资","是否发放"};
        for (int i =0 ; i < row2Cell.length ; i++ ){
            row2.createCell(i).setCellValue(row2Cell[i]);
        }
        //3.3创建第三行及单元格
        if(payList!= null && payList.size()>0){
            for(int j=0 ; j<payList.size() ;j++){
                HSSFRow rowUser = sheet.createRow(j+1);
                System.out.println(payList.get(j).getAt());
                rowUser.createCell(0).setCellValue(payList.get(j).getId());
                rowUser.createCell(1).setCellValue(payList.get(j).getAccount());
                rowUser.createCell(2).setCellValue(payList.get(j).getUsername());
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                String dateString = format.format(payList.get(j).getPaytime());
                rowUser.createCell(3).setCellValue(dateString);
                rowUser.createCell(4).setCellValue(payList.get(j).getBasewage());
                rowUser.createCell(5).setCellValue(payList.get(j).getPaywage());
                rowUser.createCell(6).setCellValue(payList.get(j).getAt());
                rowUser.createCell(7).setCellValue(payList.get(j).getBooleanpay());
            }
        }
        hwb.write(outputStream);
    }

    public PageInfo<Pay> selectAll(String page, String limit, String account) {
        int pu = Integer.parseInt(page);
        int li = Integer.parseInt(limit);
        PageHelper.startPage(pu,li);
        List<Pay> list = payMapper.selectAll1(account);
        list = this.util(list);
        PageInfo<Pay> pageInfo = new PageInfo<Pay>(list);
        return pageInfo;
    }

    public List<Pay> selectByAccount(String account) {
        return payMapper.selectByAccount(account);
    }
}
